package com.crm.service.impl.customerManager;

import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerCustomMapper;
import com.crm.dao.mapper.customMapper.orderManagerCustomMapper.CustomerProductOrderCustomMapper;
import com.crm.model.qo.memberManager.MonthDealCustomerQO;
import com.crm.model.qo.memberManager.MonthNewMemberQO;
import com.crm.model.vo.customerManager.NewSingleMemberStatisticsVO;
import com.crm.model.vo.productManager.ProductNamesVo;
import com.crm.service.customerManager.NewMemberAnalyseDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author luojie
 * @date 2021/5/25 13:38
 */
@Service
public class NewMemberAnalyseDbServiceImpl implements NewMemberAnalyseDbService {

    @Autowired
    private CustomerCustomMapper customerCustomMapper;
    @Autowired
    private CustomerProductOrderCustomMapper customerProductOrderCustomMapper;

    /**
     * 获取新增会员数
     *
     * @param companyId 公司id
     * @param yearMonth 年月
     * @return
     */
    @Override
    public long getNewMemberMonthlyStatistics(String companyId, String yearMonth) {
        long result = customerCustomMapper.selectMemberMonthlyStatistics(companyId, yearMonth);
        return result;
    }


    @Override
    public List<MonthNewMemberQO> selectMemberEveryMonthlyStatistics(String companyId, String startDate, String endDate) {
        return customerCustomMapper.selectMemberEveryMonthlyStatistics(companyId, startDate, endDate);
    }

    /**
     * 成交客户数
     *
     * @param companyId 公司id
     * @param yearMonth 年月
     * @return
     */
    @Override
    public long getDealCustomersNumber(String companyId, String yearMonth) {
        long result = customerProductOrderCustomMapper.selectDealCustomersNumber(companyId, yearMonth);
        return result;
    }

    @Override
    public List<MonthDealCustomerQO> getEveryMonthDealCustomersNumber(String companyId, String startDate, String endDate) {

        return customerProductOrderCustomMapper.getEveryMonthDealCustomersNumber(companyId, startDate, endDate);
    }

    @Override
    public List<NewSingleMemberStatisticsVO> getNewSingleMemberStatistics(String yearMonth, String companyId) {
        List<NewSingleMemberStatisticsVO> newSingleMemberStatisticsVOS = customerProductOrderCustomMapper.selectNewSingleMemberStatistics(yearMonth, companyId);
        return newSingleMemberStatisticsVOS;
    }

    @Override
    public List<NewSingleMemberStatisticsVO> selectNewSingleMemberStatisticsByContract(String yearMonth, String companyId, String contractType) {
        List<NewSingleMemberStatisticsVO> newSingleMemberStatisticsVOS = customerProductOrderCustomMapper.selectNewSingleMemberStatisticsByContract(yearMonth, companyId, contractType);
        if (newSingleMemberStatisticsVOS != null) {
            List<String> collect = newSingleMemberStatisticsVOS.stream().map(NewSingleMemberStatisticsVO::getProductOrderDetailId).collect(Collectors.toList());
            List<ProductNamesVo> productNamesVos = customerProductOrderCustomMapper.selectProductNameByOrderId(collect);
            Map<String, String> collect1 = productNamesVos.stream().collect(Collectors.toMap(ProductNamesVo::getProductOrderDetailId, ProductNamesVo::getProductNames));
            for (NewSingleMemberStatisticsVO statisticsVo : newSingleMemberStatisticsVOS
            ) {
                statisticsVo.setOrderDetailName(collect1.get(statisticsVo.getProductOrderDetailId()));
            }
        }
        return newSingleMemberStatisticsVOS;
    }

    @Override
    public long selectThisMonthPercentageQuotaStatistics(String yearMonth, String companyId, String contractType) {
        long result = customerProductOrderCustomMapper.selectThisMonthPercentageQuotaStatistics(yearMonth, companyId, contractType);
        return result;
    }
}
